/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.logging.*;
import javax.swing.*;

/**
 *
 * @author Lone_Wolf
 */
public class CreerNouvClient extends JPanel {
    
    private Fenetre fenetre;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JLabel lblNom;
    private JLabel lblCode;
    private JLabel lblTitre;
    private JLabel lblExisteDeja;
    private JTextField txtNom;
    private JTextField txtCode;
    private JButton btnCreer;
    private JButton btnAnnuler;
    
    public CreerNouvClient(Fenetre fenetre) {
        this.fenetre = fenetre;
        initComposantes();
        buildInterface();
        
        fenetre.setTitle("Nouveau Client");
        //fenetre.setSize(400, 300);
        
        requestFirstFocus();
    }
    
    private void requestFirstFocus() {
        fenetre.addWindowFocusListener(new WindowAdapter() {

            public void windowGainedFocus(WindowEvent e) {
                txtNom.requestFocusInWindow();
            }
        });
        JFrame trick = new JFrame();
        trick.setVisible(true);
        trick.setVisible(false);
        fenetre.requestFocus();
    }
    
    private void initComposantes() {
        lblTitre = new JLabel("Nouveau Client");
        lblTitre.setFont(f);
        
        lblNom = new JLabel("Entrer le nom du client : ");
        lblCode = new JLabel("Entrer le code du client : ");
        lblExisteDeja = new JLabel("Ce client existe déjà!");
        lblExisteDeja.setForeground(Color.red);
        lblExisteDeja.setVisible(false);
        
        txtNom = new JTextField();
        txtCode = new JTextField();
        
        btnCreer = new JButton("Créer");
        btnAnnuler = new JButton("Annuler");
        
        btnCreer.addActionListener(new GererEvenements());
        btnAnnuler.addActionListener(new GererEvenements());
        
        txtNom.addKeyListener(new GererFocus());
        txtCode.addKeyListener(new GererFocus());
    }
    
    private void buildInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;
        
        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.0;
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblTitre, gbc);
        this.add(lblTitre);
        
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                  //Bloc Nom
        gbl.setConstraints(lblNom, gbc);
        this.add(lblNom);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtNom, gbc);
        this.add(txtNom);
        
        gbc.gridwidth = gbc.REMAINDER;      //bords + Separateur
        makeBorder();
        makeSeparator();
        makeBorder();
        
        gbc.weightx = 0.1;                  //Bloc Code
        gbl.setConstraints(lblCode, gbc);
        this.add(lblCode);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtCode, gbc);
        this.add(txtCode);
        
        gbc.gridwidth = gbc.REMAINDER;      //bords
        makeBorder();
        makeSeparator();
        makeSeparator();
        
        makeBorder();                       //boutons
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(btnAnnuler, gbc);
        gbl.setConstraints(btnCreer, gbc);
        this.add(btnAnnuler);
        makeBorder();
        gbl.setConstraints(btnCreer, gbc);
        this.add(btnCreer);
        makeBorder();
        makeSeparator();
        makeSeparator();
        
        makeBorder();
        makeBorder();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblExisteDeja, gbc);
        this.add(lblExisteDeja);
        makeBorder();
    }
    
    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }
    
    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }
    
    private boolean validerDonnees() {
        boolean valide = false;
        btnCreer.setForeground(Color.darkGray);
        lblExisteDeja.setVisible(false);
        
        if (!txtNom.getText().isEmpty()) {
            lblNom.setForeground(Color.darkGray);
            if (!txtCode.getText().isEmpty()) {
                lblCode.setForeground(Color.darkGray);
                if (!alreadyExists()) {
                    valide = true;
                } else{
                    btnCreer.setForeground(Color.red);
                    lblExisteDeja.setVisible(true);
                }
            } else {
                lblCode.setForeground(Color.red);
                txtCode.requestFocusInWindow();
            }
        } else {
            lblNom.setForeground(Color.red);
            txtNom.requestFocusInWindow();
        }
        return valide;
    }
    
    private boolean alreadyExists() {
        boolean exists = false;
        Client clTemp;
        
        for (int i = 0; i < fenetre.arClient.size(); i++) {
            clTemp = fenetre.arClient.get(i);
            if (clTemp.getNom().equals(txtNom.getText()) || clTemp.getCode().equals(txtCode.getText())) {
                exists = true;
            }
        }
        
        return exists;
    }
    
    class GererEvenements implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnCreer) {
                if (validerDonnees()) {
                    fenetre.arClient.add(new Client(txtNom.getText(), txtCode.getText()));
                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();
                        
                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else if (e.getSource() == btnAnnuler) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    class GererFocus implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        /**
         * permet de changer le focus d'un champ de texte (JTextField) à un autre grâce à la touche "Entrée".
         * @param e
         * permet d'itentifier la source dans laquelle une touche a étée relâchée.
         */
        public void keyReleased(KeyEvent e) {
            if (e.getSource() == txtNom) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtCode.requestFocusInWindow();
                }
            } else if (e.getSource() == txtCode) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    btnCreer.doClick();
                }
            }
        }
    }
}
